#include <iostream>
using std::cout;
using std::endl;

class Singleton{
public:
    class AutoRelease{
    public:
        AutoRelease()
        { cout << "AutoRelease(Singleton*)" << endl; }

        //内部类能够访问外部类的静态数据成员
        //内部类不能访问外部类的非静态数据成员
        //因为访问非静态数据成员是通过this指针实现的
        //而此时this指针指向的是内部类本对象
        ~AutoRelease(){
            /* cout << _ix << endl; */
            cout << "~AutoRelease()" << endl;
            if(_pInstance){
                delete _pInstance;
                _pInstance = nullptr;
            }
        }
    };
    static Singleton * getInstance(){
        if(_pInstance == nullptr){
            _pInstance = new Singleton;
        }
        return _pInstance;
    }

    void init(int x,int y){
        _ix = x;
        _iy = y;
    }
    void print() const{
        cout << "(" << _ix << "," 
            << _iy << ")" << endl;
    }
    /* static void destroy(){ */
    /*     if(_pInstance){ */
    /*         delete _pInstance; */
    /*         _pInstance = nullptr; */
    /*     } */
    /* } */
   
private:
    Singleton() = default;
    ~Singleton(){ cout << "~Singleton()" << endl; }
private:
    int _ix;
    int _iy;
    static Singleton* _pInstance;
    //静态的对象成员
    static AutoRelease _ar;
};
Singleton* Singleton::_pInstance = nullptr;
Singleton::AutoRelease Singleton::_ar;


void test0(){
    /* Singleton * p = Singleton::getInstance(); */
    /* p->init(1,2); */
    /* p->print(); */
    Singleton::getInstance()->init(100,200);
    Singleton::getInstance()->print();
}

int main(void){
    test0();
    return 0;
}
